package com.team.six.bluecatshop.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.team.six.bluecatshop.domain.*;
import com.team.six.bluecatshop.mapper.ProductTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Override
    @Transactional
    public void add(ProductType productType) {
        productTypeMapper.insert(productType);
    }

    @Override
    @Transactional
    public void remove(String id) {
        productTypeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public ProductType findById(String id) {
        ProductType productType=productTypeMapper.selectByPrimaryKey(id);
        return productType;
    }

    @Override
    public List<ProductType> findAll() {
        List<ProductType> productTypeList = productTypeMapper.findAll();
        return productTypeList;
    }

    @Override
    public ProductType findProductTypeByName(String name) {
        ProductType productType = productTypeMapper.findByName(name);
        return productType;
    }

    @Override
    public List<ProductType> findProductTypeByParentId(String id) {
        List<ProductType> productType = productTypeMapper.findByParentId(id);
        return productType;
    }


    @Override
    @Transactional
    public void updateByPrimaryKey(ProductType productType) {
        productTypeMapper.updateByPrimaryKeySelective(productType);
    }

    @Override
    public List<Product> findByProductTypeId(String id) {
        return productTypeMapper.findByProductTypeId(id);
    }

    /**
     * 加载商品分类,写成通用方法
     */
    @Override
    public List<ProductType> fenlei() {
        List<ProductType> productTypeList=findProductTypeByParentId("");//把一级查询出来
        for (ProductType p:productTypeList ) {
            List<ProductType> productTypeChildList=findProductTypeByParentId(p.getPtypeId());//根据一级id将二级查询出来，存在一级里
            p.setProductTypeChildren(productTypeChildList);
            for (ProductType p1:productTypeChildList) {
                List<ProductType> productTypeChildChildList=findProductTypeByParentId(p1.getPtypeId());//根据二级id将三级查询出来，存在一级里
                p1.setProductTypeChildren(productTypeChildChildList);
            }
        }
        return productTypeList;
    }

    @Override
    public ProductType selectByPrimaryKey(String id) {
        return productTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public SeachProductTypeResponse findProductTypeAll(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        ProductTypeExample example = new ProductTypeExample();
        example.createCriteria().andPtypeNameLike("%"+keyword+"%");
        List<ProductType> productTypes = productTypeMapper.selectByExample(example);
        for (ProductType productType : productTypes){
            if (productType.getPtypeParent().equals("")){
                continue;
            }else {
                ProductType productType2 = productTypeMapper.selectByPrimaryKey(productType.getPtypeParent());
                if (productType2!=null){
                    productType.setPtypeParentName(productType2.getPtypeName());
                }else {
                    continue;
                }
            }
        }
        return new SeachProductTypeResponse(((Page)productTypes).getTotal(),productTypes);
    }
}
